// dllmain.cpp
// (c) 2008, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "stdafx.h"
#include "resource.h"
#include "RemoveReadOnly_i.h"
#include "dllmain.h"
#include "dlldatax.h"

CRemoveReadOnlyModule _AtlModule;

//
// Called to register our COM server.
// We will use this opportunity to "approve" our shell extension.
//
// @param bRegTypeLib Whether to register the typelib or not.
// @return COM result code.
//
HRESULT CRemoveReadOnlyModule::DllRegisterServer( BOOL bRegTypeLib /*= TRUE*/ ) throw()
{
    HRESULT hRes = CAtlDllModuleT< CRemoveReadOnlyModule >::DllRegisterServer(bRegTypeLib);
    if (SUCCEEDED(hRes)) {
        CRegKey approvedKey;
        approvedKey.Create(HKEY_LOCAL_MACHINE,
                           _T("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"));
        if (approvedKey.m_hKey != 0) {
            approvedKey.SetStringValue(_T("{88503209-51E7-4FC5-8C34-C5EABF403E9F}"), _T("RemoveReadOnly Contextual Menu Handler"));
        } else {
            hRes = E_ACCESSDENIED;
        }
    }
    return hRes;
}

//
// Called to unregister our COM server.
// We will use this opportunity to "unapprove" our shell extensions.
//
// @param bUnRegTypeLib Whether to unregister the typelib or not.
// @return COM result code.
//
HRESULT CRemoveReadOnlyModule::DllUnregisterServer( BOOL bUnRegTypeLib /*= TRUE*/ ) throw()
{
    HRESULT hRes = CAtlDllModuleT< CRemoveReadOnlyModule >::DllUnregisterServer(bUnRegTypeLib);
    if (SUCCEEDED(hRes)) {
        CRegKey approvedKey;
        approvedKey.Open(HKEY_LOCAL_MACHINE,
                         _T("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"));
        if (approvedKey.m_hKey != 0) {
            approvedKey.DeleteValue(_T("{88503209-51E7-4FC5-8C34-C5EABF403E9F}"));
        }
    }
    return hRes;
}

// DLL Entry Point
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
#ifdef _MERGE_PROXYSTUB
	if (!PrxDllMain(hInstance, dwReason, lpReserved))
		return FALSE;
#endif
	hInstance;
	return _AtlModule.DllMain(dwReason, lpReserved); 
}
